package com.zenithsun.pic.picreview.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.syzg.common.service.impl.BaseServiceImpl;
import com.syzg.common.util.PKCreator;
import com.syzg.core.page.DefaultPageResult;
import com.syzg.core.page.PageResult;
import com.zenithsun.common.bean.ModelType;
import com.zenithsun.common.review.model.Review;
import com.zenithsun.common.review.model.ReviewState;
import com.zenithsun.news.newsinfo.model.Newsinfo;
import com.zenithsun.pic.picreview.dao.PicReviewDAO;
import com.zenithsun.pic.picreview.model.PicReview;
import com.zenithsun.pic.picreview.service.PicReviewService;
import com.zenithsun.pic.picsubject.model.Picsubject;

@Service("picReviewService")
public class PicReviewServiceImpl extends BaseServiceImpl<PicReview> implements PicReviewService {
    
    private PicReviewDAO picReviewDao = null;

    @Resource
	public void setreviewDAO(PicReviewDAO picReviewDao) {
		this.setDAO(picReviewDao);
		this.picReviewDao = picReviewDao;
	}
    /**
	 * 保存意见
	 * @param ps
	 * @param toCheck 是否提交审核
	 * @return
	 * @throws Exception
	 */
    @Override
	public boolean saveReview(Picsubject ps, boolean toCheck)
			throws Exception {
    	Review review = this.picReviewDao.getReviewByBus(ps.getId(),
				ModelType.Pic.toString());
		if (review == null) {
			return addReview(ps, toCheck);
		} else {
			if (toCheck) {
				review.setState(ReviewState.WaitReview.toShort());
				this.picReviewDao.update(review);
			}
			return true;
		}
	}
    /**
	 * 保存意见
	 * @param ps
	 * @param toCheck 是否提交审核
	 * @return
	 * @throws Exception
	 */
    @Override
    public boolean addReview(Picsubject ps, boolean toCheck) throws Exception{
    	// TODO Auto-generated method stub
    	
    	try {
    		Review review = new Review();
			review.setBusId(ps.getId());
			review.setBusMark(ModelType.Pic.toString());
			review.setCreateTime(new Date());
			review.setId(PKCreator.getPk());
			review.setMark(Short.valueOf("0"));
			if (toCheck) {
				review.setState(ReviewState.WaitReview.toShort());
			} else {
				review.setState(ReviewState.SaveNotReview.toShort());
			}
			picReviewDao.create(review);
		} catch (Exception e) {
			
			//return false;
			e.printStackTrace();
		}
		return true;
    }
    /**
	 * 查询审核图片库
	 * @param pageNo 当前页
	 * @param pageSize 也行数
	 * @param searchCondition
	 * @return
	 * @throws Exception
	 */
    @Override
	public PageResult queryPicSubjectByState(int pageNo, int pageSize,PicReview searchCondition) throws Exception {
		int totalCount = this.picReviewDao.countPicReview(searchCondition);
		List items = this.picReviewDao.listPicReview(pageNo, pageSize,searchCondition);
		PageResult pageResult = new DefaultPageResult(items, totalCount,(pageNo - 1) * pageSize, pageSize);
		return pageResult;
	}
    
    @Override
    public HashMap<String, String> findPicReview(String id) throws Exception {
    	// TODO Auto-generated method stub
    	return picReviewDao.findPicReview(id);
    }
}
